// 注意: 本项目的所有源文件都必须是 UTF-8 编码
#include <Python.h>
#include <iostream>
#include <mirai.h>
#include "myheader.h"
#include <stdio.h>
#include <string>
//#include "windows.h"
//#include <sstream>
using namespace std;
using namespace Cyan;

int main()
{
#if defined(WIN32) || defined(_WIN32)
	// 切换代码页，让 CMD 可以显示 UTF-8 字符
	system("chcp 65001");
#endif

	MiraiBot bot("127.0.0.1", 8080);
	while (true)
	{
		try
		{
			bot.Auth("1145141919810", 3117048803_qq);
			break;
		}
		catch (const std::exception& ex)
		{
			cout << ex.what() << endl;
		}
		MiraiBot::SleepSeconds(1);
	}
	cout << "Bot Working..." << endl;

	bool flag = true;
	bool& flag_cite = flag;

	bot.On<GroupMessage>([&](GroupMessage m)
		{
			try
			{
				auto mc = m.MessageChain;
				string plain = mc.GetPlainText();
				if (plain == "来张日榜图"&&flag_cite == true)
				{
					flag_cite = false;
					m.Reply(mc.Plain("——正在搜寻日榜图"));
					string img_name = _down(1) + ".png";
					cout << img_name << endl;
					//ifstream _if;
					//string temp_result;
					//_if.open("E:/学习相关/C++/testCode/mirai-cpp-template/result.txt");
					//_if >> temp_result;
					string file_address = "E:/学习相关/C++/testCode/mirai-cpp-template/temp_img/";
					file_address.append(img_name);
					cout << file_address << endl;
					GroupImage gImg = bot.UploadGroupImage(file_address);
					bot.SendMessage(m.Sender.Group.GID, MessageChain().Image(gImg));
					remove(file_address.c_str());
					cout << "删除文件成功" << endl;
					m.Reply(mc.Plain("——20秒冷却ing"));
					Sleep(20000);
					m.Reply(mc.Plain("——冷却完成"));
					flag_cite = true;
					//WIN32_FIND_DATA FindFileData;
					//HANDLE hFind;
					//char* img_cache = "D:/mirai/data/net.mamoe.mirai-api-http/images";
					//hFind = FindFirstFile(img_cache, &FindFileData);
					//if (hFind == INVALID_HANDLE_VALUE) {
					//	cout << "错误句柄" << endl;
					//}
					//else {
					//	stringstream img_cache_address;
					//	img_cache_address << img_cache << FindFileData.cFileName;
					//	string combined = img_cache_address.str();
					//	cout << combined << endl;
					//	remove(combined.c_str());
					//	cout << "清理缓存成功" << endl;
					//	FindClose(hFind);
					//}
					//cout << "10秒冷却ing" << endl;
					//bot.SendMessage(m.Sender.Group.GID, MessageChain().Plain("10秒冷却ing"));
				}
				if (plain == "来张周榜图" && flag_cite == true)
				{
					flag_cite = false;
					m.Reply(mc.Plain("——正在搜寻周榜图"));
					string img_name = _down(2) + ".png";
					cout << img_name << endl;
					string file_address = "E:/学习相关/C++/testCode/mirai-cpp-template/temp_img/";
					file_address.append(img_name);
					cout << file_address << endl;
					GroupImage gImg = bot.UploadGroupImage(file_address);
					bot.SendMessage(m.Sender.Group.GID, MessageChain().Image(gImg));
					remove(file_address.c_str());
					cout << "删除文件成功" << endl;
					m.Reply(mc.Plain("——20秒冷却ing"));
					Sleep(20000);
					m.Reply(mc.Plain("——冷却完成"));
					flag_cite = true;
				}
				if (plain == "来张月榜图" && flag_cite == true)
				{
					flag_cite = false;
					m.Reply(mc.Plain("——正在搜寻月榜图"));
					string img_name = _down(3) + ".png";
					cout << img_name << endl;

					string file_address = "E:/学习相关/C++/testCode/mirai-cpp-template/temp_img/";
					file_address.append(img_name);
					cout << file_address << endl;
					GroupImage gImg = bot.UploadGroupImage(file_address);
					bot.SendMessage(m.Sender.Group.GID, MessageChain().Image(gImg));
					remove(file_address.c_str());
					cout << "删除文件成功" << endl;
					m.Reply(mc.Plain("——20秒冷却ing"));
					Sleep(20000);
					m.Reply(mc.Plain("——冷却完成"));
					flag_cite = true;
				}
				if (plain == "来张日榜涩图" && flag_cite == true)
				{
					flag_cite = false;
					m.Reply(mc.Plain("——正在搜寻日榜涩图"));
					string img_name = _down(4) + ".png";
					cout << img_name << endl;

					string file_address = "E:/学习相关/C++/testCode/mirai-cpp-template/temp_img/";
					file_address.append(img_name);
					cout << file_address << endl;
					GroupImage gImg = bot.UploadGroupImage(file_address);
					bot.SendMessage(m.Sender.Group.GID, MessageChain().Image(gImg));
					remove(file_address.c_str());
					cout << "删除文件成功" << endl;
					m.Reply(mc.Plain("——20秒冷却ing"));
					Sleep(20000);
					m.Reply(mc.Plain("——冷却完成"));
					flag_cite = true;
				}
				if (plain == "来张周榜涩图" && flag_cite == true)
				{
					flag_cite = false;
					m.Reply(mc.Plain("——正在搜寻周榜涩图"));
					string img_name = _down(5) + ".png";
					cout << img_name << endl;

					string file_address = "E:/学习相关/C++/testCode/mirai-cpp-template/temp_img/";
					file_address.append(img_name);
					cout << file_address << endl;
					GroupImage gImg = bot.UploadGroupImage(file_address);
					bot.SendMessage(m.Sender.Group.GID, MessageChain().Image(gImg));
					remove(file_address.c_str());
					cout << "删除文件成功" << endl;
					m.Reply(mc.Plain("——20秒冷却ing"));
					Sleep(20000);
					m.Reply(mc.Plain("——冷却完成"));
					flag_cite = true;
				}
				if (plain == "更新所有榜单" && flag_cite == true)
				{
					flag_cite = false;
					m.Reply(mc.Plain("——更新中，请稍后"));
					bool update = _search();
					if (update == true)
					{
						cout << "更新成功" << endl;
						m.Reply(mc.Plain("——更新完成"));
					}
					m.Reply(mc.Plain("——20秒冷却ing"));
					Sleep(20000);
					m.Reply(mc.Plain("——冷却完成"));
					flag_cite = true;
				}
				//if (plain == "测试")
				//{
				//	GroupImage gImg = bot.UploadGroupImage("E:/学习相关/C++/testCode/mirai-cpp-template/temp_img/23.webp");
				//	bot.SendMessage(m.Sender.Group.GID, MessageChain().Image(gImg));
				//}
			}
			catch (const std::exception& ex)
			{
				cout << ex.what() << endl;
			}
		});
	bot.EventLoop();
	return 0;
}